package com.shycloud.mido.common.security.util;

import com.happy.zebra.admin.api.entity.SysUserRole;
import com.shycloud.mido.common.security.service.ShyMember;
import com.shycloud.mido.common.security.service.ShyOpterMember;
import com.shycloud.mido.common.security.service.ShyOrganMember;
import com.shycloud.mido.common.security.service.ShyUser;
import lombok.experimental.UtilityClass;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * 安全工具类
 *
 * @author L.cm
 */
@UtilityClass
public class SecurityUtils {

	/** 默认密码 */
	public static final String DEFAULT_PASSWORD = "123456";

	/**
	 * 获取Authentication
	 */
	public Authentication getAuthentication() {
		return SecurityContextHolder.getContext().getAuthentication();
	}

	/**
	 * 获取会员
	 *
	 * @param authentication
	 * @return Member
	 * <p>
	 * 获取当前用户的全部信息 EnableShyResourceServer true 获取当前用户的用户名 EnableShyResourceServer false
	 */
	public ShyMember getMember(Authentication authentication) {
		Object principal = authentication.getPrincipal();
		if (principal instanceof ShyMember) {
			return (ShyMember) principal;
		}
		return null;
	}

	/**
	 * 获取用户
	 *
	 * @param authentication
	 * @return ShyUser
	 * <p>
	 * 获取当前用户的全部信息 EnableShyResourceServer true 获取当前用户的用户名 EnableShyResourceServer false
	 */
	public ShyUser getUser(Authentication authentication) {
		Object principal = authentication.getPrincipal();
		if (principal instanceof ShyUser) {
			return (ShyUser) principal;
		} else if (principal instanceof String) {
			return new ShyUser(0, 0, 0,
					(String) principal, "", 0, false,
					false, false,
					false, new ArrayList<SimpleGrantedAuthority>());
		}
		return null;
	}

	/**
	 * 获取用户
	 *
	 * @param authentication
	 * @return ShyOrganMember
	 * <p>
	 * 获取当前用户的全部信息 EnableShyResourceServer true 获取当前用户的用户名 EnableShyResourceServer false
	 */
	public ShyOrganMember getOrganMemeber(Authentication authentication) {
		Object principal = authentication.getPrincipal();
		if (principal instanceof ShyOrganMember) {
			return (ShyOrganMember) principal;
		}
		return null;
	}

	/**
	 * 获取用户
	 */
	public ShyOrganMember getOrganMemeber() {
		Authentication authentication = getAuthentication();
		return getOrganMemeber(authentication);
	}

	/**
	 * 获取用户
	 */
	public ShyUser getUser() {
		Authentication authentication = getAuthentication();
		return getUser(authentication);
	}

	/**
	 * 获取会员
	 */
	public ShyMember getMember() {
		Authentication authentication = getAuthentication();
		return getMember(authentication);
	}

	/**
	 * 获取运营商
	 */
	public ShyOpterMember getOperatorMember() {
		Authentication authentication = getAuthentication();
		return getOperatorMember(authentication);
	}

	/**
	 * 获取运营商
	 *
	 * @param authentication
	 * @return ShyOperatorMember
	 * <p>
	 * 获取当前用户的全部信息 EnableShyResourceServer true 获取当前用户的用户名 EnableShyResourceServer false
	 */
	public ShyOpterMember getOperatorMember(Authentication authentication) {
		Object principal = authentication.getPrincipal();
		if (principal instanceof ShyOpterMember) {
			return (ShyOpterMember) principal;
		}
		return null;
	}

	/**
	 * 获取用户角色信息
	 *
	 * @return 角色集合
	 */
	public List<Integer> getRoles(Integer roleId) {

		List<Integer> roleIds = new ArrayList<>();

		Integer userId = SecurityUtils.getUser().getId();

		//获取此用户下的所有权限ID

		roleIds.add(roleId);
		return roleIds;
	}

}
